from read_file import find_station, read_line
from time import localtime
from math import ceil, floor
import heapq


# from line import Station,Line
# 广度优先搜索
def find_line_way(line_dict: dict, start_line_name, end_line_name):
    temp_graph = {}
    for key in line_dict.keys():
        temp_line = line_dict[key]
        temp_graph[temp_line.name] = list(temp_line.neighbours.keys())
    temp_queue = [start_line_name]
    result = {}
    visited = []
    while temp_queue:
        temp_line_name = temp_queue.pop(0)
        # print(temp_line_name)
        # if temp_line_name == end_line_name:
        #     break
        # temp_line = line_dict[temp_line_name]
        for item in temp_graph[temp_line_name]:
            if item not in visited:
                visited.append(item)
                result[item] = temp_line_name
                temp_queue.append(item)
    # print(result)
    return result


def find_trans_stations(line_dict: dict, line_name_set: list, start_station_name: str, end_station_name: str,
                        start_time: int):
    # temp_plan = {
    #     "lines": [],
    #     "start_station_name": start_station_name,
    #     "end_station_name": end_station_name,
    #     "trans_stations": [],
    #     "now_time": now_time,
    #     "start_time": 0,
    #     "end_time": 0,
    #     "whole_time": 0
    # }
    temp_graph = {}
    time_graph = {}
    # if not line_dict[line_name_set[0]].stations[start_station_name].isTrans:# 如果起始站不是换乘站
    time_graph[start_station_name] = start_time
    temp_graph[start_station_name] = line_dict[line_name_set[0]].neighbours[line_name_set[1]]
    for i in range(0, len(line_name_set)):
        if i < len(line_name_set) - 2:
            # 如果不是最后一个,也不是倒数第二个
            temp_line = line_dict[line_name_set[i]]
            for trans_stations_name in temp_line.neighbours[line_name_set[i + 1]]:
                temp_graph[trans_stations_name] = line_dict[line_name_set[i + 1]].neighbours[line_name_set[i + 2]]
        elif i < len(line_name_set) - 1:
            temp_line = line_dict[line_name_set[i]]
            for trans_stations_name in temp_line.neighbours[line_name_set[i + 1]]:
                temp_graph[trans_stations_name] = [end_station_name]
    temp_graph[end_station_name] = []
    # print(temp_graph)
    # 计算带权重的有向图
    for key in list(temp_graph.keys()):
        time_graph[key] = {}
        for station in temp_graph[key]:
            plans = find_inner_way(line_dict, key, station)
            time_graph[key][station] = list(plans['plans'].values())[0]['time']
    # print(time_graph)
    # 初始化距离和前驱字典
    distance = {station: float('inf') for station in time_graph}
    distance[start_station_name] = 0
    prev = {station: None for station in time_graph}

    # 使用优先队列（最小堆）
    heap = []
    heapq.heappush(heap, (0, start_station_name))

    while heap:
        current_dist, current_station = heapq.heappop(heap)
        # 当前路径非最短时跳过
        if current_dist > distance[current_station]:
            continue
        # 遍历邻居
        for neighbor, weight in time_graph[current_station].items():
            new_dist = current_dist + weight
            if new_dist < distance[neighbor]:
                distance[neighbor] = new_dist
                prev[neighbor] = current_station
                heapq.heappush(heap, (new_dist, neighbor))
        # 若处理到终点，可提前退出（Dijkstra特性）
        if current_station == end_station_name:
            break

    # 回溯路径
    path = []
    current = end_station_name
    while current is not None:
        path.append(current)
        current = prev[current]
    path.reverse()  # 逆序得到起点到终点的路径
    # print(path)
    # print("总时间:", distance[end_station_name])
    return path
    # temp_queue = [start_station_name]
    # result = {}
    # visited = []
    # while temp_queue:
    #     temp_station_name = temp_queue.pop(0)
    #     for item in temp_graph[temp_station_name]:
    #         if item not in visited:
    #             visited.append(item)
    #             result[item] = temp_station_name
    #             temp_queue.append(item)
    # print(result)
    # final_path = [end_station_name]
    # now_station_name = end_station_name
    # while now_station_name != start_station_name:
    #     final_path.append(result[now_station_name])
    #     now_station_name = result[now_station_name]
    # final_path.reverse()
    # print(final_path)
    # return final_path


def find_inner_way(line_dict: dict, start_station_name: str, end_station_name: str):
    """

    :param line_dict: 线路字典
    :param start_station_name: 始发站名称
    :param end_station_name: 到达站名称
    :return: result = {
        'inLine': False,
        'plans':{}
    }
    """
    # 分别获取始发站和终点站的信息
    # station_info = {
    #     "isFind": False,
    #     "lines": []
    # }
    result = {
        'inLine': False,
        'plans': {}
    }
    start_info = find_station(start_station_name, line_dict)
    end_info = find_station(end_station_name, line_dict)
    if start_info["isFind"] == False or end_info["isFind"] == False:
        return result
    for start_line_name in start_info['lines']:
        # 遍历起始站的所有线路
        start_line = line_dict[start_line_name]
        for end_line_name in end_info['lines']:
            if start_line_name == end_line_name:
                result['inLine'] = True
                # 如果有相同的线路 解决平行边问题
                temp_plan = {
                    'time': 0,
                    'dist': 0
                }
                if start_station_name == end_station_name:
                    result['plans'][start_line_name] = temp_plan
                    return result
                # 计算时间与长度
                # 向两个方向遍历,直至找到终点站
                temp_station = start_line.stations[start_station_name]
                # print(temp_station.name,temp_station.neighbours)
                # 如果不是终点站
                if len(temp_station.neighbours) > 1:
                    # 第一个方向：邻接表字典中下标为0的元素
                    back = list(temp_station.neighbours.keys())[1]
                    direction1 = {
                        'visited_station': [temp_station.name, back],
                        'time': 0,
                        'dist': 0
                    }  # temp_station.neighbours[temp_station.neighbours.keys()[0]]['dist']
                    first_flag = True
                    while True:
                        for next_station_name in temp_station.neighbours.keys():
                            if next_station_name not in direction1['visited_station']:
                                # 如果未被访问
                                direction1['visited_station'].append(next_station_name)
                                direction1['time'] += (60 + temp_station.neighbours[next_station_name]['time'])
                                direction1['dist'] += temp_station.neighbours[next_station_name]['dist']
                                temp_station = start_line.stations[next_station_name]
                        if first_flag:
                            direction1['visited_station'].remove(back)
                            first_flag = False
                        if temp_station.name == end_station_name:
                            # print(direction1)
                            direction1['time'] -= 60
                            break
                        if len(temp_station.neighbours) == 1:
                            # 如果到终点站时还没有找到
                            direction1 = {}
                            break

                    # 第二个方向：邻接表字典中下标为1的元素
                    temp_station = start_line.stations[start_station_name]
                    back = list(temp_station.neighbours.keys())[0]
                    direction2 = {
                        'visited_station': [temp_station.name, back],
                        'time': 0,
                        'dist': 0
                    }  # temp_station.neighbours[temp_station.neighbours.keys()[0]]['dist']
                    # print(direction2['visited_station'])
                    first_flag = True
                    while True:
                        for next_station_name in temp_station.neighbours.keys():
                            if next_station_name not in direction2['visited_station']:
                                # 如果未被访问
                                direction2['visited_station'].append(next_station_name)
                                direction2['time'] += (60 + temp_station.neighbours[next_station_name]['time'])
                                direction2['dist'] += temp_station.neighbours[next_station_name]['dist']
                                temp_station = start_line.stations[next_station_name]
                        if first_flag:
                            direction2['visited_station'].remove(back)
                            first_flag = False
                        if temp_station.name == end_station_name:
                            # print(direction2)
                            direction2['time'] -= 60
                            break
                        if len(temp_station.neighbours) == 1:
                            # 如果到终点站时还没有找到
                            direction2 = {}
                            break
                else:
                    temp_station = start_line.stations[start_station_name]
                    direction2 = {}
                    direction1 = {
                        'visited_station': [temp_station.name],
                        'time': 0,
                        'dist': 0
                    }  # temp_station.neighbours[temp_station.neighbours.keys()[0]]['dist']
                    while True:
                        for next_station_name in temp_station.neighbours.keys():
                            if next_station_name not in direction1['visited_station']:
                                # 如果未被访问
                                direction1['visited_station'].append(next_station_name)
                                direction1['time'] += (60 + temp_station.neighbours[next_station_name]['time'])
                                direction1['dist'] += temp_station.neighbours[next_station_name]['dist']
                                temp_station = start_line.stations[next_station_name]
                        if temp_station.name == end_station_name:
                            direction1['time'] -= 60
                            break
                        if len(temp_station.neighbours) == 1:
                            # 如果到终点站时还没有找到
                            # print(direction1)
                            direction1 = {}
                            break
                if direction1 != {} and direction2 != {}:
                    # 遇到环线了
                    if direction1['time'] <= direction2['time']:
                        temp_plan['time'] = direction1['time']
                        temp_plan['dist'] = direction1['dist']
                        temp_plan['visited_station'] = direction1['visited_station']
                    else:
                        temp_plan['time'] = direction2['time']
                        temp_plan['dist'] = direction2['dist']
                        temp_plan['visited_station'] = direction2['visited_station']
                else:  # 正常情况
                    if direction1 == {}:
                        temp_plan['time'] = direction2['time']
                        temp_plan['dist'] = direction2['dist']
                        temp_plan['visited_station'] = direction2['visited_station']
                    elif direction2 == {}:
                        temp_plan['time'] = direction1['time']
                        temp_plan['dist'] = direction1['dist']
                        temp_plan['visited_station'] = direction1['visited_station']
                result['plans'][start_line_name] = temp_plan
    if result['inLine']:
        plans = result['plans']
        # 按照time的值进行排序
        sorted_plans = dict(sorted(plans.items(), key=lambda item: item[1]['time']))
        # 更新data中的plans
        result['plans'] = sorted_plans
    return result


def deal_with_loop_time(line_dict: dict, now_line_name: str, station_name: str):
    """
    计算两个始发站到目标站的距离与时间
    :param line_dict:
    :param line_name:
    :param station_name:
    :return:
    """
    result = {
        "dir1": {
            "time": 0,
            "dist": 0
        },
        "dir2": {
            "time": 0,
            "dist": 0
        }
    }
    now_line = line_dict[now_line_name]
    start_station_dir1_name = list(now_line.stations.keys())[0]
    start_station_dir1 = now_line.stations[start_station_dir1_name]
    start_station_dir2_name = list(now_line.stations.keys())[-1]
    start_station_dir2 = now_line.stations[start_station_dir2_name]
    if station_name != start_station_dir1_name:
        # dir1
        temp_plan = {"visited_stations": [start_station_dir1_name, start_station_dir2_name], "time": 0, "dist": 0}
        # 执行完第一轮后把start_station_dir2_name拿掉
        now_station = now_line.stations[start_station_dir1_name]
        flag = True
        while True:
            for next_station_name in list(now_station.neighbours.keys()):
                if next_station_name not in temp_plan["visited_stations"]:
                    temp_plan["time"] += (60 + now_station.neighbours[next_station_name]["time"])
                    temp_plan["dist"] += now_station.neighbours[next_station_name]["dist"]
                    temp_plan["visited_stations"].append(next_station_name)
                    now_station = now_line.stations[next_station_name]
                    break
            if flag:
                flag = False
                temp_plan["visited_stations"].remove(start_station_dir2_name)
            if now_station.name == station_name or now_station.name == start_station_dir2_name:
                temp_plan["time"] -= 60
                break
        result['dir1'] = temp_plan

    if station_name != start_station_dir2_name:
        # dir2
        temp_plan = {"visited_stations": [start_station_dir1_name, start_station_dir2_name], "time": 0, "dist": 0}
        # 执行完第一轮后把start_station_dir2_name拿掉
        now_station = now_line.stations[start_station_dir2_name]
        flag = True
        while True:
            for next_station_name in list(now_station.neighbours.keys()):
                if next_station_name not in temp_plan["visited_stations"]:
                    temp_plan["time"] += (60 + now_station.neighbours[next_station_name]["time"])
                    temp_plan["dist"] += now_station.neighbours[next_station_name]["dist"]
                    temp_plan["visited_stations"].append(next_station_name)
                    now_station = now_line.stations[next_station_name]
                    break
            if flag:
                flag = False
                temp_plan["visited_stations"].remove(start_station_dir1_name)
            if now_station.name == station_name or now_station.name == start_station_dir1_name:
                temp_plan["time"] -= 60
                break
        result['dir2'] = temp_plan

    return result


def find_inner_way_time(line_dict: dict, start_station_name: str, end_station_name: str, now_time: int):
    """

    :param line_dict:
    :param start_station_name:
    :param end_station_name:
    :return:
    """
    result = {
        "plans": []
    }
    # time_struct = localtime()
    # 当前秒数
    # now_time = time_struct.tm_hour * 3600 + time_struct.tm_min * 60 + time_struct.tm_sec
    # now_time = 70000
    start_to_end = find_inner_way(line_dict, start_station_name, end_station_name)
    if not start_to_end['inLine']:
        return result
    temp_plan = {
        "lines": [],
        "start_station_name": start_station_name,
        "end_station_name": end_station_name,
        "trans_stations": [],
        "now_time": now_time,
        "start_time": 0,
        "end_time": 0,
        "whole_time": 0,
        "dist": 0,
        'visited_station': []
    }
    # 如果是在同一条线上
    temp_line_name = list(start_to_end['plans'].keys())[0]
    temp_line = line_dict[temp_line_name]
    if temp_line.isLoop == 0:
        # 如果不是环路
        dir1_to_start = find_inner_way(line_dict, list(temp_line.stations.keys())[0], start_station_name)
        # dir1_to_start = find_inner_way(line_dict,"古城", start_station_name)

        dir1_to_end = find_inner_way(line_dict, list(temp_line.stations.keys())[0], end_station_name)
        dir2_to_start = find_inner_way(line_dict, list(temp_line.stations.keys())[-1], start_station_name)
        dir2_to_end = find_inner_way(line_dict, list(temp_line.stations.keys())[-1], end_station_name)
        # print(dir1_to_end)
        if temp_line_name not in list(dir1_to_start['plans'].keys()):
            dir1_to_start['plans'][temp_line_name] = dir1_to_start['plans'][list(dir1_to_start['plans'].keys())[0]]
        if temp_line_name not in list(dir1_to_end['plans'].keys()):
            # 因为如果起点站就是dir1的话，线路名称可能是别的名字
            dir1_to_end['plans'][temp_line_name] = dir1_to_end['plans'][list(dir1_to_end['plans'].keys())[0]]
        # 如果没有if, dir1_to_end 在这里被错误赋值了！！！
        if temp_line_name not in list(dir2_to_start['plans'].keys()):
            dir2_to_start['plans'][temp_line_name] = dir2_to_start['plans'][list(dir2_to_start['plans'].keys())[0]]
        if temp_line_name not in list(dir2_to_end['plans'].keys()):
            dir2_to_end['plans'][temp_line_name] = dir2_to_end['plans'][list(dir2_to_end['plans'].keys())[0]]
        # print(list(temp_line.stations.keys())[0])
        # print(dir1_to_end)
        # print(dir2_to_end)
        # print(temp_line_name)
        # 没有解决十三号线从头到尾的问题,现在解决了
        if dir1_to_end['plans'][temp_line_name]['dist'] == dir1_to_start['plans'][temp_line_name]['dist'] + \
                start_to_end['plans'][temp_line_name]['dist']:
            # 方向为dir1
            is_stop = False
            temp_depart_time = now_time - dir1_to_start['plans'][temp_line_name]['time']
            if temp_depart_time < temp_line.dir1First:  # 如果未发车
                start_time = temp_line.dir1First + dir1_to_start['plans'][temp_line_name]['time']
            elif temp_depart_time > temp_line.dir1Last:  # 如果是末班车
                start_time = 0
                is_stop = True
            else:
                depart_num = floor((now_time - temp_line.dir1First) / temp_line.gapTime)
                if depart_num == ceil((now_time - temp_line.dir1First) / temp_line.gapTime):
                    # 如果是正好这个时间点
                    start_time = now_time
                else:
                    start_time = temp_line.dir1First + temp_line.gapTime * (depart_num + 1)
            # 向下取整的发车次数
            end_time = start_time + start_to_end['plans'][temp_line_name]['time']
            temp_plan['lines'].append(temp_line_name)
            temp_plan['start_time'] = start_time
            temp_plan['end_time'] = end_time
            temp_plan['whole_time'] = end_time - now_time
            temp_plan['dist'] += start_to_end['plans'][temp_line_name]['dist']
            temp_plan['visited_station'] = (start_to_end['plans'][temp_line_name]['visited_station'])
            if not is_stop:
                result['plans'].append(temp_plan)
        elif dir2_to_end['plans'][temp_line_name]['dist'] == dir2_to_start['plans'][temp_line_name]['dist'] + \
                start_to_end['plans'][temp_line_name]['dist']:
            # 方向为dir2
            # 向下取整的发车次数
            is_stop = False
            temp_depart_time = now_time - dir1_to_start['plans'][temp_line_name]['time']
            if temp_depart_time < temp_line.dir1First:  # 如果未发车
                start_time = temp_line.dir1First + dir1_to_start['plans'][temp_line_name]['time']
            elif temp_depart_time > temp_line.dir1Last:  # 如果是末班车
                start_time = 0
                is_stop = True
            else:
                depart_num = floor((now_time - temp_line.dir2First) / temp_line.gapTime)
                if depart_num == ceil((now_time - temp_line.dir2First) / temp_line.gapTime):
                    # 如果是正好这个时间点
                    start_time = now_time
                else:
                    start_time = temp_line.dir2First + temp_line.gapTime * (depart_num + 1)
            end_time = start_time + start_to_end['plans'][temp_line_name]['time']
            temp_plan['lines'].append(temp_line_name)
            temp_plan['start_time'] = start_time
            temp_plan['end_time'] = end_time
            temp_plan['whole_time'] = end_time - now_time
            temp_plan['dist'] += start_to_end['plans'][temp_line_name]['dist']
            temp_plan['visited_station'] = (start_to_end['plans'][temp_line_name]['visited_station'])
            if not is_stop:
                result['plans'].append(temp_plan)
    else:  # 如果是环路
        temp_dirs = deal_with_loop_time(line_dict, temp_line_name, start_station_name)
        dir1_to_start = temp_dirs['dir1']
        dir2_to_start = temp_dirs['dir2']
        temp_dirs = deal_with_loop_time(line_dict, temp_line_name, end_station_name)
        dir1_to_end = temp_dirs['dir1']
        dir2_to_end = temp_dirs['dir2']
        if start_station_name in dir1_to_end['visited_stations']:
            # 方向为dir1
            is_stop = False
            temp_depart_time = now_time - dir1_to_start['time']
            if temp_depart_time < temp_line.dir1First:  # 如果未发车
                start_time = temp_line.dir1First + dir1_to_start['time']
            elif temp_depart_time > temp_line.dir1Last:  # 如果是末班车
                start_time = 0
                is_stop = True
            else:
                depart_num = floor((now_time - temp_line.dir1First) / temp_line.gapTime)
                if depart_num == ceil((now_time - temp_line.dir1First) / temp_line.gapTime):
                    # 如果是正好这个时间点
                    start_time = now_time
                else:
                    start_time = temp_line.dir1First + temp_line.gapTime * (depart_num + 1)
            # 向下取整的发车次数
            end_time = start_time + start_to_end['plans'][temp_line_name]['time']
            temp_plan['lines'].append(temp_line_name)
            temp_plan['start_time'] = start_time
            temp_plan['end_time'] = end_time
            temp_plan['whole_time'] = end_time - now_time
            temp_plan['dist'] += start_to_end['plans'][temp_line_name]['dist']
            temp_plan['visited_station'] = (start_to_end['plans'][temp_line_name]['visited_station'])
            if not is_stop:
                result['plans'].append(temp_plan)
        elif start_station_name in dir2_to_end['visited_stations']:
            # 方向为dir2
            # 向下取整的发车次数
            is_stop = False
            temp_depart_time = now_time - dir1_to_start['time']
            if temp_depart_time < temp_line.dir1First:  # 如果未发车
                start_time = temp_line.dir1First + dir1_to_start['time']
            elif temp_depart_time > temp_line.dir1Last:  # 如果是末班车
                start_time = 0
                is_stop = True
            else:
                depart_num = floor((now_time - temp_line.dir2First) / temp_line.gapTime)
                if depart_num == ceil((now_time - temp_line.dir2First) / temp_line.gapTime):
                    # 如果是正好这个时间点
                    start_time = now_time
                else:
                    start_time = temp_line.dir2First + temp_line.gapTime * (depart_num + 1)
            end_time = start_time + start_to_end['plans'][temp_line_name]['time']
            temp_plan['lines'].append(temp_line_name)
            temp_plan['start_time'] = start_time
            temp_plan['end_time'] = end_time
            temp_plan['whole_time'] = end_time - now_time
            temp_plan['dist'] += start_to_end['plans'][temp_line_name]['dist']
            temp_plan['visited_station'] = (start_to_end['plans'][temp_line_name]['visited_station'])
            if not is_stop:
                result['plans'].append(temp_plan)
    return result


def find_whole_way(line_dict: dict, start_station_name: str, end_station_name: str):
    """

    :param line_dict:
    :param start_station_name: 起始站
    :param end_station_name: 结束站
    :return: {"plans":[temp_plan = {
            "lines": plan_lines,
            "start_station_name": start_station_name,
            "end_station_name": end_station_name,
            "trans_stations": [],
            "now_time": start_time,
            "start_time": 0,
            "end_time": 0,
            "whole_time": 0
        }]}
    """
    result = {
        "plans": [],
        "message": "station not found"
    }
    time_struct = localtime()
    # 当前秒数
    start_time = time_struct.tm_hour * 3600 + time_struct.tm_min * 60 + time_struct.tm_sec
    # start_time = 1000000
    # 如果没有找到该点
    if not (find_station(start_station_name, line_dict)['isFind'] and find_station(end_station_name, line_dict)[
        'isFind']):
        return result
    start_to_end_test = find_inner_way(line_dict, start_station_name, end_station_name)
    temp_plan = {
        "lines": [],
        "start_station_name": start_station_name,
        "end_station_name": end_station_name,
        "trans_stations": [],
        "now_time": start_time,
        "start_time": 0,
        "end_time": 0,
        "whole_time": 0
    }
    if start_to_end_test['inLine']:
        if find_inner_way_time(line_dict, start_station_name, end_station_name, start_time)['plans']:
            temp_plan = find_inner_way_time(line_dict, start_station_name, end_station_name, start_time)['plans'][0]
            temp_plan['visited_station'] = [temp_plan['visited_station']]
            result['plans'].append(temp_plan)
        # return result
        # find_inner_way_time会筛选出最快的单线线路
    start_line_set = find_station(start_station_name, line_dict)['lines']
    end_line_set = find_station(end_station_name, line_dict)['lines']
    # 对于每一个起始线路
    plan_line_set = []
    for start_line_name in start_line_set:
        # 先计算始发站到换乘站的时间
        # 初始化
        # 预处理
        for end_line_name in end_line_set:
            way_map = find_line_way(line_dict, start_line_name, end_line_name)
            way_line_name_set = [end_line_name]  # 储存经过的线路
            temp_line_name = end_line_name
            while temp_line_name != start_line_name:
                temp_line_name = way_map[temp_line_name]
                way_line_name_set.append(temp_line_name)
            way_line_name_set.reverse()
            plan_line_set.append(way_line_name_set)
    for item in plan_line_set:
        if len(item) == 1:
            plan_line_set.remove(item)
    # 移除直达路线
    # print(plan_line_set)
    info_set = []
    for plan_lines in plan_line_set:
        temp_plan = {
            "lines": plan_lines,
            "start_station_name": start_station_name,
            "end_station_name": end_station_name,
            "trans_stations": [],
            "now_time": start_time,
            "start_time": 0,
            "end_time": 0,
            "whole_time": 0,
            "dist": 0,
            'visited_station': []
        }
        temp_info = {}
        start_line_name = plan_lines[0]
        start_line = line_dict[start_line_name]
        now_line = line_dict[start_line_name]
        now_line_name = start_line_name
        now_time = start_time
        # 遍历每一个方案
        # 每个方案内有限的换乘站作为节点，采用dijkstra算法
        temp_path = find_trans_stations(line_dict, plan_lines, start_station_name, end_station_name, now_time)
        # print(temp_path)
        temp_plan['trans_stations'] = temp_path[1:-1]
        for i in range(0, len(temp_path)):
            if i < len(temp_path) - 1:
                # print(temp_path[i], temp_path[i + 1])
                temp_result = find_inner_way_time(line_dict, temp_path[i], temp_path[i + 1], now_time)
                # print(temp_result)
                if not temp_result['plans']:
                    # 如果为空，说明已经超时
                    return {'plans': [], 'message': 'out of time'}
                temp_plan['visited_station'].append(temp_result['plans'][0]['visited_station'])
                temp_plan['dist'] += temp_result['plans'][0]['dist']
                if i == 0:
                    # temp_plan['dist'] = 0
                    temp_plan['start_time'] = temp_result['plans'][0]['start_time']
                now_time = temp_result['plans'][0]['end_time'] + 300
                if i < len(temp_path) - 2:
                    temp_plan['whole_time'] += (300 + temp_result['plans'][0]['whole_time'])
                else:  # 倒数第二站
                    temp_plan['whole_time'] += (temp_result['plans'][0]['whole_time'])
                    end_time = temp_result['plans'][0]['end_time']
            else:
                temp_plan['end_time'] = end_time
        result['plans'].append(temp_plan)
    result['message'] = 'succeed'
    return result


if __name__ == '__main__':
    # paths = []
    lineDict = read_line()
    # 环路上相邻的死循环,为什么?
    # 答案在find_inner_way里面
    # print(find_inner_way(lineDict, "长椿街", "复兴门"))
    # our_result = find_inner_way_time(lineDict, "大钟寺", "西直门",50000)
    our_result = find_whole_way(lineDict, "大钟寺", "西直门")
    print(our_result)
